package stack;

import java.util.Arrays;

public class MyStack implements IStack{

    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_CAPACITY = 10;

    public MyStack(){
        elem = new int[DEFAULT_CAPACITY];
    }


    @Override
    public int push(int x) {
        if (full()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = x;
        return x;

    }

    @Override
    public int pop() {
//        int ret = peek();
//        usedSize--;
//        return ret;
        if (empty()){
            //return -1;
            throw new EmptyException("栈空了");
        }
        int oldVal = elem[--usedSize];
        return oldVal;
    }

    @Override
    public int peek() {
        if (empty()){
            //return -1;
            throw new EmptyException("栈空了");
        }
        return elem[usedSize-1];
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public boolean empty() {
        return usedSize < 0;
    }

    @Override
    public boolean full() {
        return usedSize == elem.length;
    }
}
